/*
 * Copyright 2010 Electronic Business Systems Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gr.ebs.gssdroid.widget;

import gr.ebs.gssdroid.R;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class CustomListAdapter<T> extends BaseAdapter{
	protected Context context;
	
	private ArrayList<T> listItems = new ArrayList<T>();
	private LayoutInflater layoutInflater;
	private int mViewId;

	public CustomListAdapter(Context context, int viewid, ArrayList<T> listItems) {
		layoutInflater = LayoutInflater.from(context);
		mViewId = viewid;
		this.listItems = listItems;
		this.context = context;
	}
	
	public ArrayList<T> getListItems() {
		return listItems;
	}

	public int getCount() {
		return listItems.size();
	}

	public T getItem(int position) {
		return listItems.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	public void setListItems(ArrayList<T> listItems) {
		this.listItems = listItems;
	}

	@SuppressWarnings("unchecked")
	public View getView(int position, View view, ViewGroup parent) {
		if (view == null) {
			view = layoutInflater.inflate(mViewId, null);
		}
		//parent.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
	    //view.setFocusable(true);
	    //view.requestFocus();
		bindView(position, view);
		//view.setClickable(true);
	    //view.setFocusable(true);
		return view;
	}
	
	protected int getIconForMime(String mimetype){
		if (mimetype == null)
			return R.drawable.filegeneric;
		mimetype = mimetype.toLowerCase();
		if (mimetype.startsWith("application/pdf"))
			return R.drawable.acroread;
		else if (mimetype.endsWith("excel"))
			return R.drawable.spreadsheet;
		else if (mimetype.endsWith("msword"))
			return R.drawable.office;
		else if (mimetype.endsWith("powerpoint"))
			return R.drawable.presentation;
		else if (mimetype.startsWith("application/zip") ||
					mimetype.startsWith("application/gzip") ||
					mimetype.startsWith("application/x-gzip") ||
					mimetype.startsWith("application/x-tar") ||
					mimetype.startsWith("application/x-gtar"))
			return R.drawable.zip;
		/*else if (mimetype.startsWith("text/html"))
			return R.drawable.html;*/
		else if (mimetype.startsWith("text/plain"))
			return R.drawable.text;
		else if (mimetype.startsWith("image/"))
			return R.drawable.image;
		else if (mimetype.startsWith("video/"))
			return R.drawable.video;
		else if (mimetype.startsWith("audio/"))
			return R.drawable.audio;
		return R.drawable.filegeneric;
	}
	
	protected abstract void bindView(int position, View view);
}
